﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public partial class BossListScene : Scene
{
	const string SUPPRESS_BOSS_LIST_CELL_NAME = "Cell - Suppress Boss List";
	
	Dictionary<int, RaidBoss> suppressBossList = new Dictionary<int, RaidBoss>();
	Dictionary<int, GameObject> suppressBossObjectList = new Dictionary<int, GameObject>();

	public GameObject suppressBossListCell;
	public UIScrollView suppressBossListScrollView;
	public UIGrid suppressBossListGrid;


	public void SwitchSuppressBossCell(UIToggle toggle)
	{
		EPToggle epToggle = toggle.GetComponent<EPToggle>();
		epToggle.OnChangeProcess(UIToggle.current.value ? true : false);
		
		Debug.Log ("SwitchTab : " + epToggle.toggleIndex);

		if (!UIToggle.current.value)
			return;

		if (suppressBossList.Count > 0)
			ActivateRaidBossModel(suppressBossList[toggle.gameObject.GetComponent<SuppressBossListCell>().indexPath].raidBossId);
	}
	
	void LoadSuppressBossList()
	{
		NetworkManager.Instance.C2S_RAIDBOSS_LIST(2, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}
			
			GameObject g = null;
			int idx = 0;
			
			RemoveAllSuppressBossList();
			
			if (responseData.data.Count <= 0)
			{
				DeactivateRaidBossModel();
				NGUITools.SetActive(suppressBossBlankView, true);
				return;
			}
			
			NGUITools.SetActive(suppressBossBlankView, false);
			
			foreach (RaidBoss rb in responseData.data)
			{
				AddRaidBossModelObject(rb.raidBossId);

				g = NGUITools.AddChild(suppressBossListGrid.transform.gameObject, suppressBossListCell);
				g.transform.localScale = Vector3.one;
				g.name = SUPPRESS_BOSS_LIST_CELL_NAME;
				
				SuppressBossListCell spc = g.GetComponent<SuppressBossListCell>();
				spc.indexPath = idx;
				spc.bossNameLabel.text = string.Format( "Lv{0} {1}", rb.lv, Localization.Get("bossName" + rb.raidBossId.ToString()) );
				spc.finderNameLabel.text = rb.name;
				spc.successObject.SetActive(false);
				spc.failureObject.SetActive(false);
				if (rb.hp > 0)
					spc.failureObject.SetActive(true);
				else
					spc.successObject.SetActive(true);

				suppressBossObjectList.Add (idx, g);
				suppressBossList.Add (idx, rb);
				
				idx++;
			}

			suppressBossObjectList[0].GetComponent<SuppressBossListCell>().toggle.value = true;
			suppressBossListGrid.Reposition();
			suppressBossListScrollView.ResetPosition();
		});
	}

	/*
	void LoadSuppressBossList()
	{
		GameObject g = null;
		int idx = 0;
		NGUITools.SetActive(suppressBossBlankView, false);		
		for (int i = 0; i < 10; i++)
		{
			g = NGUITools.AddChild(suppressBossListGrid.transform.gameObject, suppressBossListCell);
			g.transform.localScale = Vector3.one;
			g.name = SUPPRESS_BOSS_LIST_CELL_NAME;
			SuppressBossListCell spc = g.GetComponent<SuppressBossListCell>();
			spc.indexPath = idx;
			spc.bossNameLabel.text = string.Format( "Lv{0} {1}", 1, Localization.Get("bossName1004") );
			spc.finderNameLabel.text = "보스 이름";
			spc.successObject.SetActive(false);
			spc.failureObject.SetActive(true);
			suppressBossObjectList.Add (idx, g);
			idx++;
		}
		suppressBossListGrid.Reposition();
		suppressBossListScrollView.ResetPosition();
	}
	*/

	void RemoveAllSuppressBossList()
	{
		suppressBossList.Clear();
		
		foreach (KeyValuePair<int, GameObject> pair in suppressBossObjectList)
		{
			NGUITools.SetActive(pair.Value, false);
			NGUITools.Destroy(pair.Value);
		}
		suppressBossObjectList.Clear();
	}

	public void OnClick_PopupDetailButtonProccess(int selectedIndexPath)
	{
		RaidBoss selectedRaidBoss = suppressBossList[selectedIndexPath];

		Popup_SuppressList ps = PopupManager.PushPopup(PopupKind.SuppressList) as Popup_SuppressList;

		ps.successTitleObject.SetActive(false);
		ps.failureTitleObject.SetActive(false);
		if (selectedRaidBoss.hp > 0)
			ps.failureTitleObject.SetActive(true);
		else
			ps.successTitleObject.SetActive(true);

		ps.selectedRaidPartyMemberList = selectedRaidBoss.party;
		ps.bossNameLabel.text = Localization.Get("bossName" + selectedRaidBoss.raidBossId.ToString());
		ps.finderNameLabel.text = selectedRaidBoss.name;
		ps.LoadPartyMemberList();
	}

}
